package com.baimawangzi.recovery.dao.impl;

import com.baimawangzi.recovery.bean.Activity;
import com.baimawangzi.recovery.dao.ActivityDao;
import com.baimawangzi.recovery.util.dbutil.BeanHandler;
import com.baimawangzi.recovery.util.dbutil.OnlyHandler;

import java.util.List;

public class ActivityDaoImpl extends BaseDaoImpl<Activity>implements ActivityDao {
    public ActivityDaoImpl() {
        clazz= Activity.class;
        init();
    }
    @Override
    public List<Activity> map(String sql, Object[] obj) {
        List<Activity> activities = queryRunner.query(sql, new BeanHandler<Activity>(clazz), obj);

        return activities;
    }
    @Override
    public List<Activity> findByLimit(int m, int n) {
        String sql="select *from t_activity limit ?,?";
        Object[] object= {m, n};
        List<Activity> activities = queryRunner.query(sql, new BeanHandler<Activity>(clazz), object);
        return activities;
    }

    @Override
    public long getCount() {
        String sql="select count(*) totalSize from t_activity where ?=?";
        Object[] object= {1, 1};
        List<Object> onlylist = queryRunner.query(sql,new OnlyHandler(),object);
        return (long) onlylist.get(0);
    }

    @Override
    public int add(Activity activity) {
        //insert into t_activity values (null,'11111','11111111111111111',DATE ('2011-09-08'),'进行中');
        String sql="insert into t_activity values (null,?,?,DATE (?),?)";
        Object[] objects={activity.getTitle(),activity.getContent(),activity.getStarttime(),activity.getStat()};
        return queryRunner.update(sql,objects);
    }

    @Override
    public int delete(int id) {
        String sql ="delete from t_activity where id=?";
        Object[] obj={id};
        return queryRunner.update(sql,obj);
    }

    @Override
    public Activity findById(int id) {
        String sql="select *from t_activity where id = ?";
        Object[] object= {id};
        Activity activity = queryRunner.query(sql, new BeanHandler<Activity>(clazz), object).get(0);

        return activity;
    }

    @Override
    public int update(Activity activity) {
        //update t_activity set title='wwwww',content='11',starttime=DATE('2019-09-08'),stat='进行中' where id=1;
        String sql="update t_activity set title=?,content=?,starttime=DATE(?),stat=? where id=?";
        Object[] object= {activity.getTitle(),activity.getContent(),activity.getStarttime(),activity.getStat(),activity.getId()};
        return queryRunner.update(sql,object);
    }
}
